﻿using ASPNETVN.PORTAL.Components.UI;
using System;
using System.Linq;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.Helpers;
using System.Web.UI.WebControls;
using System.Collections;
using System.Web.UI;
using ASPNETVN.PORTAL.Components.Share;
using System.Collections.Generic;
using ASPNETVN.PORTAL.Components.Share.Controls;
using System.Data;

namespace ASPNETVN.PORTAL.Modules.FEEDBACK.HOME.Controls
{
    public partial class ItemManager : ModulePage
    {
        private static List<FEEDBACK.Components.Item> _listItems = new List<FEEDBACK.Components.Item>();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!PortalSecurity.HasManageAuthorization(this.ModuleID))
            {
                PortalSecurity.AccessDenied();
            }

            if (IsPostBack == false)
            {
                LoadData();
            }
        }

        public void LoadData()
        {
            Guid moduleID = new Guid(this.Settings["FEEDBACK.ModuleID"].ToString());

            if (moduleID == Guid.Empty)
            {
                ContentPane.Controls.Add(new LiteralControl("Chưa cấu hình FEEDBACK.ModuleID"));
                return;
            }

            BuildPageLink();

            if (Request.QueryString["categoryid"] != null)
            {
                //gvFeedback.DataSource = Components.Item.SelectCollectionDynamic("CategoryID = " + Convert.ToInt32(Request.QueryString["categoryid"]), "");
                _listItems = LoadData(moduleID, Convert.ToInt32(Request.QueryString["categoryid"])).OrderByDescending(x => x.CreatedDate).ToList();
            }
            else
            {
                _listItems = Components.Item.SelectCollectionAll(moduleID).OrderByDescending(x => x.CreatedDate).ToList();
            }

            gvFeedback.DataSource = _listItems;
            gvFeedback.DataBind();
        }

        private List<FEEDBACK.Components.Item> LoadData(Guid moduleID, int categoryID)
        {
            List<FEEDBACK.Components.Item> list = new List<FEEDBACK.Components.Item>();

            IList<Category> allCategories = new List<Category>();
            allCategories = Category.SelectCollectionAll(moduleID);
            IList<Category> topCategories = allCategories.Where(x => x.ID == categoryID).OrderBy(x => x.Title).ToList();

            foreach (Category topCategory in topCategories)
            {
                list.AddRange(FEEDBACK.Components.Item.SelectCollectionBy(moduleID, topCategory.ID));

                IList<Category> childCategories = allCategories.Where(x => x.ParentID == topCategory.ID).OrderBy(x => x.Title).ToList();

                foreach (Category childCategory in childCategories)
                {
                    list.AddRange(FEEDBACK.Components.Item.SelectCollectionBy(moduleID, childCategory.ID));
                }
            }

            return list;
        }

        private void BuildPageLink()
        {
            if (Request.QueryString["CategoryID"] != null)
            {
                int categoryID = Convert.ToInt32(Request.QueryString["CategoryID"]);
                PageLink pagelink = (PageLink)this.ParentControl.FindControl("MainPageLink");
                Category category = Category.Load(categoryID);
                if (category != null)
                {
                    Category parentCategory = Category.Load(category.ParentID);
                    if (parentCategory != null)
                    {
                        pagelink.AddLink(parentCategory.Title, ResolveClientUrl(HttpUrlBuilder.BuildModulePageUrl(this.ModuleID, 0, ModulePages.ItemManager, "categoryid={0}", parentCategory.ID)));

                        string url = ResolveClientUrl(HttpUrlBuilder.BuildModulePageUrl(this.ModuleID, 0, ModulePages.ItemManager, "categoryid={0}", category.ID));
                        pagelink.AddLink(category.Title, url);
                    }
                    else
                    {
                        pagelink.AddLink(category.Title, ResolveClientUrl(HttpUrlBuilder.BuildModulePageUrl(this.ModuleID, 0, ModulePages.ItemManager, "categoryid={0}", category.ID)));
                    }
                }
            }
        }

        protected void gvFeedback_SelectedIndexChanged(object sender, EventArgs e)
        {
            Guid moduleID = new Guid(this.Settings["FEEDBACK.ModuleID"].ToString());

            if (moduleID == Guid.Empty)
            {
                ContentPane.Controls.Add(new LiteralControl("Chưa cấu hình FEEDBACK.ModuleID"));
                return;
            }

            string url = HttpUrlBuilder.BuildAddEditUrl(moduleID, Convert.ToInt32(gvFeedback.SelectedValue), "FeedbackDetail");
            Response.Redirect(url);
        }

        protected void gvFeedback_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Components.Item.DeleteItem(Convert.ToInt32(gvFeedback.DataKeys[e.RowIndex].Value));
            LoadData();
        }

        protected void gvFeedback_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvFeedback.PageIndex = e.NewPageIndex;
            LoadData();
        }

        protected void gvFeedback_PageIndexChanged(object sender, EventArgs e)
        {
            gvFeedback.SelectedIndex = -1;
        }
    }
}